package cn.nubia.admin.application.controller;

import cn.nubia.admin.application.query.SoftQuery;
import cn.nubia.admin.application.service.SoftService;
import cn.nubia.admin.sysconfig.bean.CategoryQuery;
import cn.nubia.admin.sysconfig.service.CategoryService;
import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.springmvc.Response;
import cn.nubia.admin.application.bean.NotifyPush;
import cn.nubia.admin.application.query.NotifyPushQuery;
import cn.nubia.admin.application.service.NotifyPushService;
import cn.nubia.admin.application.query.TopicQueryParam;
import cn.nubia.admin.application.service.TopicService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created by justin on 15/7/15.
 */
@Controller
@RequestMapping(value = "/admin/notify")
public class NotifyPushController {
    @Autowired
    NotifyPushService notifyPushService;
    @Autowired
    SoftService softService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    TopicService topicService;

    @RequiresPermissions(value = "soft:client:notify:list")
    @RequestMapping(value = "list")
    public ModelAndView list(NotifyPushQuery notifyPushQuery){
        ModelAndView modelAndView = new ModelAndView("/notify/notifyList");
        Paged<NotifyPush> notifyPushPaged = notifyPushService.listNotifyPushes(notifyPushQuery);
        modelAndView.addObject("query",notifyPushQuery);
        modelAndView.addObject("notifies",notifyPushPaged);
        return modelAndView;
    }
    @RequestMapping(value = "edit")
    public ModelAndView add(HttpServletRequest request,Integer id){
        ModelAndView modelAndView = new ModelAndView("/notify/editNotify");
        NotifyPush notifyPush =new NotifyPush();
        if(id!=null){
            notifyPush = notifyPushService.getNotify(id);
            if(notifyPush.getMsgObj() == NotifyPush.MSGOBJ_APP) {
                SoftQuery softQuery = new SoftQuery();
                softQuery.setId(notifyPush.getResId());
                modelAndView.addObject("checkSofts", softService.findSoftList(softQuery));
            }else if(notifyPush.getMsgObj() == NotifyPush.MSGOBJ_CATETORY){
                CategoryQuery categoryQuery = new CategoryQuery();
                categoryQuery.setCategoryId(notifyPush.getResId());
                modelAndView.addObject("categories", categoryService.list(categoryQuery));
            }else if(notifyPush.getMsgObj() == NotifyPush.MSGOBJ_TOPIC){
                TopicQueryParam topicQuery = new TopicQueryParam();
                topicQuery.setTopicId(notifyPush.getResId());
                modelAndView.addObject("topics", topicService.findPageList(topicQuery));
            }
        }
        modelAndView.addObject("notify",notifyPush);
        modelAndView.addObject("myReferer",request.getHeader("Referer"));
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping(value = "save")
    public Response<Boolean> save(@Valid NotifyPush notifyPush, BindingResult result){
        if(result.hasErrors())return Response.getFailedResponse(result.getAllErrors().get(0).getDefaultMessage());
        if(notifyPush.getIsLoopPush()==null)
            return Response.getFailedResponse("是否循环推送不能为空");
        if(notifyPush.getMsgShowType()==null)
            return Response.getFailedResponse("消息展示方式不能为空");
        if(notifyPush.getBeginTime()==null)
            return Response.getFailedResponse("开始时间不能为空");
        if(notifyPush.getIsLoopPush()==1){
            if(notifyPush.getLoopCycle()==null||notifyPush.getLoopCycle()<0)
                return Response.getFailedResponse("请选择循环周期");
        }
        if(notifyPush.getMsgShowType()==NotifyPush.STATE_MSGSHOWTYPE_NORMAL){
            if(notifyPush.getIconUrl()==null||"".equals(notifyPush.getIconUrl()))
                return Response.getFailedResponse("请上传展示图标");
        }else if(notifyPush.getMsgShowType()==NotifyPush.STATE_MSGSHOWTYPE_BANNER){
            if(notifyPush.getPicUrl()==null||"".equals(notifyPush.getPicUrl()))
                return Response.getFailedResponse("请上传banne图地址");
        }
        notifyPushService.saveNotify(notifyPush);
        return Response.getSuccessResponse(true);
    }

    @RequestMapping(value = "delete")
    public String  delete(Integer id){
        notifyPushService.deleteNotify(id);
        return "redirect:list.html";
    }
}
